package com.crazyant;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
 
public class DatabaseHandler extends SQLiteOpenHelper {
 
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;
 
    // Database Name
    private static final String DATABASE_NAME = "contactsManager";
 
    // Contacts table name
    private static final String TABLE_CONTACTS = "contacts";
    private static final String TABLE_RANKING = "ranking";
    private static final String TABLE_ANT = "ant";
 
    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_USER = "user";
    private static final String KEY_PASS = "pass";
    private static final String KEY_MUNDO = "mundo";
    private static final String KEY_PANTALLA = "pantalla";
    private static final String KEY_IDANT = "idant";
    private static final String KEY_SCORE = "score";
    private static final String KEY_R= "r";
    private static final String KEY_INV = "inventario";
    private static final String KEY_CASCO = "casco";
    private static final String KEY_CUERPO = "cuerpo";
    private static final String KEY_BOTAS = "botas";
    private static final String KEY_COLOR = "color";
    
 
    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
 
    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_USER  + " TEXT  UNIQUE,"
                + KEY_PASS + " TEXT," + KEY_MUNDO + " INTEGER," + KEY_PANTALLA + " INTEGER," + KEY_INV + " INTEGER, " + KEY_COLOR + " INTEGER" +")";
        db.execSQL(CREATE_CONTACTS_TABLE);
        String CREATE_RANKING_TABLE = "CREATE TABLE " + TABLE_RANKING + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_USER  + " TEXT,"
                + KEY_MUNDO + " INTEGER," + KEY_PANTALLA + " INTEGER," + KEY_SCORE + " INTEGER," + KEY_R + " INTEGER DEFAULT 0" +")";
        db.execSQL(CREATE_RANKING_TABLE);
        String CREATE_ANT_TABLE = "CREATE TABLE " + TABLE_ANT + "("
        		+ KEY_ID + " INTEGER," + KEY_USER + " TEXT," + KEY_CASCO + " INTEGER," + KEY_CUERPO + " INTEGER," + KEY_COLOR + " INTEGER,"
        		+ KEY_BOTAS + " INTEGER," 
        		+ "PRIMARY KEY (" + KEY_ID + "," + KEY_USER + "," + KEY_CASCO + "," + KEY_CUERPO + "," + KEY_COLOR + ","+ KEY_BOTAS +"),"
        		+ "FOREIGN KEY (" + KEY_USER + ") REFERENCES " + TABLE_CONTACTS + " (" + KEY_USER + ") )";
        db.execSQL(CREATE_ANT_TABLE);
    }
 
    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_RANKING);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ANT);
 
        // Create tables again
        onCreate(db);
    }
 
    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */
 
    // Adding new contact
    void addContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(KEY_USER, contact.getUser()); // Contact User
        values.put(KEY_PASS, contact.getPass()); // Contact Pass
        values.put(KEY_MUNDO, contact.getMundo());
        values.put(KEY_PANTALLA, contact.getPant());
        values.put(KEY_INV, contact.getInv());
        values.put(KEY_COLOR, contact.getIdcolor());
 
        // Inserting Row
        db.insert(TABLE_CONTACTS, null, values);
        db.close(); // Closing database connection
    }
    
    // Adding new SCORE
    void addScore(Rank rank) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(KEY_USER, rank.getUser()); // Contact User
        values.put(KEY_MUNDO, rank.getMundo()); //Contact Mundo 
        values.put(KEY_PANTALLA, rank.getPant()); //Contact Pantalla
        values.put(KEY_SCORE, rank.getScore()); //Contact Pantalla
        values.put(KEY_R, rank.getR()); //Contact Pantalla
 
        // Inserting Row
        db.insert(TABLE_RANKING, null, values);
        db.close(); // Closing database connection
    }
    
    // Adding new Ant
    void addAnt(Ant ant) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(KEY_ID, ant.getID()); // Con
        values.put(KEY_USER, ant.getUser()); // Contact User
        values.put(KEY_CASCO, ant.getCasco()); // Contact Casco
        values.put(KEY_CUERPO, ant.getCuerpo()); // Contact Cuerpo
        values.put(KEY_COLOR, ant.getColor()); // Contact Color
        values.put(KEY_BOTAS, ant.getBotas());
        
 
        // Inserting Row
        db.insert(TABLE_ANT, null, values);
        db.close(); // Closing database connection
    }

    
    // Getting single score
    	Rank getRank(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
 
        Cursor cursor = db.query(TABLE_RANKING, new String[] { KEY_ID,
                KEY_USER, KEY_MUNDO, KEY_PANTALLA, KEY_SCORE, KEY_R }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
 
        Rank ranking = new Rank(Integer.parseInt(cursor.getString(0)),
        		cursor.getString(1), Integer.parseInt(cursor.getString(2)),
        		Integer.parseInt(cursor.getString(3)), Integer.parseInt(cursor.getString(4)), Integer.parseInt(cursor.getString(5)));
        // return score
        return ranking;
    }
    
    // Getting single contact
    Contact getContact(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
 
        Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
                KEY_USER, KEY_PASS, KEY_MUNDO, KEY_PANTALLA, KEY_INV, KEY_COLOR }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
 
        Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1), cursor.getString(2), Integer.parseInt(cursor.getString(3)),
                Integer.parseInt(cursor.getString(4)),Integer.parseInt(cursor.getString(5)),Integer.parseInt(cursor.getString(6)));
        // return contact
        return contact;
    }
    
 // Getting single contact By user
    Contact getContact(String user) {
        SQLiteDatabase db = this.getReadableDatabase();
        Contact contact = new Contact();
        String selectQuery = "SELECT * FROM " + TABLE_CONTACTS + " WHERE " + KEY_USER + " = '" + user +"'";
        Cursor cursor = db.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
        	contact = new Contact(Integer.parseInt(cursor.getString(0)),
                    cursor.getString(1), cursor.getString(2), Integer.parseInt(cursor.getString(3)),
                    Integer.parseInt(cursor.getString(4)),Integer.parseInt(cursor.getString(5)),Integer.parseInt(cursor.getString(6)));
        }
 
        // return contact
        return contact;
    }
    
    // Getting Hormiga
	Ant getAnt(int id) {
	    SQLiteDatabase db = this.getReadableDatabase();
	
	    Cursor cursor = db.query(TABLE_ANT, new String[] { KEY_ID,
	            KEY_USER, KEY_CASCO, KEY_CUERPO, KEY_COLOR, KEY_BOTAS}, KEY_ID + "=?",
	            new String[] { String.valueOf(id) }, null, null, null, null);
	    if (cursor != null)
	        cursor.moveToFirst();
	
	    Ant ant = new Ant(Integer.parseInt(cursor.getString(0)),
	    		cursor.getString(1), Integer.parseInt(cursor.getString(2)),
	    		Integer.parseInt(cursor.getString(3)), Integer.parseInt(cursor.getString(4)), Integer.parseInt(cursor.getString(5)));
	    // return score
	    return ant;
	}

	// Getting single contact By user
	Ant getAnt(String user) {
        SQLiteDatabase db = this.getReadableDatabase();
        Ant ant = new Ant();
        String selectQuery = "SELECT * FROM " + TABLE_ANT + " WHERE " + KEY_USER + " = '" + user +"'";
        
        Cursor cursor = db.rawQuery(selectQuery, null);
     // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                ant.setID(Integer.parseInt(cursor.getString(0)));
                ant.setUser(cursor.getString(1));
                ant.setCasco(Integer.parseInt(cursor.getString(2)));
                ant.setCuerpo(Integer.parseInt(cursor.getString(3)));
                ant.setColor(Integer.parseInt(cursor.getString(4)));
                ant.setColor(Integer.parseInt(cursor.getString(5)));
                // Adding contact to list
            } while (cursor.moveToNext());
        }
	    // return score
	    return ant;
    }
	
	// Getting Hormiga
		Ant getAnt(int color, int cuerpo, int casco, int botas) {
		    SQLiteDatabase db = this.getReadableDatabase();
		    Ant ant = new Ant();
		    String selectQuery = "SELECT  * FROM " + TABLE_ANT + " WHERE " + KEY_COLOR + " = " + color +
		    		" AND " + KEY_CUERPO + " = " + cuerpo + " AND " + KEY_BOTAS + " = " + botas + " AND " + KEY_CASCO + " = " + casco;
		    Cursor cursor = db.rawQuery(selectQuery, null);
		    
	        // looping through all rows and adding to list
	        if (cursor.moveToFirst()) {
	            do {
	                ant.setID(Integer.parseInt(cursor.getString(0)));
	                ant.setUser(cursor.getString(1));
	                ant.setCasco(Integer.parseInt(cursor.getString(2)));
	                ant.setCuerpo(Integer.parseInt(cursor.getString(3)));
	                ant.setColor(Integer.parseInt(cursor.getString(4)));
	                ant.setColor(Integer.parseInt(cursor.getString(5)));
	                // Adding contact to list
	            } while (cursor.moveToNext());
	        }
		    // return score
		    cursor.close();
		    return ant;
		}

	
    // Getting All Contacts
    public List<Contact> getAllContacts() {
        List<Contact> contactList = new ArrayList<Contact>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact();
                contact.setID(Integer.parseInt(cursor.getString(0)));
                contact.setUser(cursor.getString(1));
                contact.setPass(cursor.getString(2));
                contact.setMundo(Integer.parseInt(cursor.getString(3)));
                contact.setPant(Integer.parseInt(cursor.getString(4)));
                contact.setInv(Integer.parseInt(cursor.getString(5)));
                contact.setIdcolor(Integer.parseInt(cursor.getString(6)));
                // Adding contact to list
                contactList.add(contact);
            } while (cursor.moveToNext());
        }
        cursor.close();
 
        // return contact list
        return contactList;
    }
    
 // Getting All Rankings 
    public List<Rank> getAllRanks() {
        List<Rank> rankList = new ArrayList<Rank>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_RANKING;
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Rank rank = new Rank();
                rank.setID(Integer.parseInt(cursor.getString(0)));
                rank.setUser(cursor.getString(1));
                rank.setMundo(Integer.parseInt(cursor.getString(2)));
                rank.setPant(Integer.parseInt(cursor.getString(3)));
                rank.setScore(Integer.parseInt(cursor.getString(4)));
                rank.setR(Integer.parseInt(cursor.getString(5)));
                // Adding contact to list
                rankList.add(rank);
            } while (cursor.moveToNext());
        }
 
        // return rank list
        return rankList;
    }
    
    // Getting All Rankings Mundo 1
    public List<Rank> getAllRanks1() {
        List<Rank> rankList = new ArrayList<Rank>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_RANKING + " WHERE " + KEY_MUNDO + "= 1 AND " + KEY_PANTALLA + " = 1 ORDER BY "+ KEY_SCORE +" ASC";
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Rank rank = new Rank();
                rank.setID(Integer.parseInt(cursor.getString(0)));
                rank.setUser(cursor.getString(1));
                rank.setMundo(Integer.parseInt(cursor.getString(2)));
                rank.setPant(Integer.parseInt(cursor.getString(3)));
                rank.setScore(Integer.parseInt(cursor.getString(4)));
                rank.setR(Integer.parseInt(cursor.getString(5)));
                // Adding contact to list
                rankList.add(rank);
            } while (cursor.moveToNext());
        }
        
        //PANTALLA 2
        selectQuery = "SELECT  * FROM " + TABLE_RANKING + " WHERE " + KEY_MUNDO + "= 1 AND " + KEY_PANTALLA + " = 2 ORDER BY "+ KEY_SCORE +" ASC";
        
        db = this.getWritableDatabase();
        cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Rank rank = new Rank();
                rank.setID(Integer.parseInt(cursor.getString(0)));
                rank.setUser(cursor.getString(1));
                rank.setMundo(Integer.parseInt(cursor.getString(2)));
                rank.setPant(Integer.parseInt(cursor.getString(3)));
                rank.setScore(Integer.parseInt(cursor.getString(4)));
                rank.setR(Integer.parseInt(cursor.getString(5)));
                // Adding contact to list
                rankList.add(rank);
            } while (cursor.moveToNext());
        }
        
        //PANTALLA 3
        selectQuery = "SELECT  * FROM " + TABLE_RANKING + " WHERE " + KEY_MUNDO + "= 1 AND " + KEY_PANTALLA + " = 3 ORDER BY "+ KEY_SCORE +" ASC";
        
        db = this.getWritableDatabase();
        cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Rank rank = new Rank();
                rank.setID(Integer.parseInt(cursor.getString(0)));
                rank.setUser(cursor.getString(1));
                rank.setMundo(Integer.parseInt(cursor.getString(2)));
                rank.setPant(Integer.parseInt(cursor.getString(3)));
                rank.setScore(Integer.parseInt(cursor.getString(4)));
                rank.setR(Integer.parseInt(cursor.getString(5)));
                // Adding contact to list
                rankList.add(rank);
            } while (cursor.moveToNext());
        }
 
        // return rank list
        return rankList;
    }
    
    // Getting All Rankings Mundo 2
    public List<Rank> getAllRanks2() {
        List<Rank> rankList = new ArrayList<Rank>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_RANKING + " WHERE " + KEY_MUNDO + "= 2 AND " + KEY_PANTALLA + " = 1 ORDER BY "+ KEY_SCORE +" ASC";
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Rank rank = new Rank();
                rank.setID(Integer.parseInt(cursor.getString(0)));
                rank.setUser(cursor.getString(1));
                rank.setMundo(Integer.parseInt(cursor.getString(2)));
                rank.setPant(Integer.parseInt(cursor.getString(3)));
                rank.setScore(Integer.parseInt(cursor.getString(4)));
                rank.setR(Integer.parseInt(cursor.getString(5)));
                // Adding contact to list
                rankList.add(rank);
            } while (cursor.moveToNext());
        }
        
        
      //PANTALLA 2
        selectQuery = "SELECT  * FROM " + TABLE_RANKING + " WHERE " + KEY_MUNDO + "= 2 AND " + KEY_PANTALLA + " = 2 ORDER BY "+ KEY_SCORE +" ASC";
        
        db = this.getWritableDatabase();
        cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Rank rank = new Rank();
                rank.setID(Integer.parseInt(cursor.getString(0)));
                rank.setUser(cursor.getString(1));
                rank.setMundo(Integer.parseInt(cursor.getString(2)));
                rank.setPant(Integer.parseInt(cursor.getString(3)));
                rank.setScore(Integer.parseInt(cursor.getString(4)));
                rank.setR(Integer.parseInt(cursor.getString(5)));
                // Adding contact to list
                rankList.add(rank);
            } while (cursor.moveToNext());
        }
        
        //PANTALLA 3
        selectQuery = "SELECT  * FROM " + TABLE_RANKING + " WHERE " + KEY_MUNDO + "= 2 AND " + KEY_PANTALLA + " = 3 ORDER BY "+ KEY_SCORE +" ASC";
        
        db = this.getWritableDatabase();
        cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Rank rank = new Rank();
                rank.setID(Integer.parseInt(cursor.getString(0)));
                rank.setUser(cursor.getString(1));
                rank.setMundo(Integer.parseInt(cursor.getString(2)));
                rank.setPant(Integer.parseInt(cursor.getString(3)));
                rank.setScore(Integer.parseInt(cursor.getString(4)));
                rank.setR(Integer.parseInt(cursor.getString(5)));
                // Adding contact to list
                rankList.add(rank);
            } while (cursor.moveToNext());
        }
 
        // return rank list
        return rankList;
    }
    
    
 // Getting All Rankings Mundo 3
    public List<Rank> getAllRanks3() {
        List<Rank> rankList = new ArrayList<Rank>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_RANKING + " WHERE " + KEY_MUNDO + "= 3 AND " + KEY_PANTALLA + " = 1 ORDER BY "+ KEY_SCORE +" ASC";
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Rank rank = new Rank();
                rank.setID(Integer.parseInt(cursor.getString(0)));
                rank.setUser(cursor.getString(1));
                rank.setMundo(Integer.parseInt(cursor.getString(2)));
                rank.setPant(Integer.parseInt(cursor.getString(3)));
                rank.setScore(Integer.parseInt(cursor.getString(4)));
                rank.setR(Integer.parseInt(cursor.getString(5)));
                // Adding contact to list
                rankList.add(rank);
            } while (cursor.moveToNext());
        }
        
        
      //PANTALLA 2
        selectQuery = "SELECT  * FROM " + TABLE_RANKING + " WHERE " + KEY_MUNDO + "= 3 AND " + KEY_PANTALLA + " = 2 ORDER BY "+ KEY_SCORE +" ASC";
        
        db = this.getWritableDatabase();
        cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Rank rank = new Rank();
                rank.setID(Integer.parseInt(cursor.getString(0)));
                rank.setUser(cursor.getString(1));
                rank.setMundo(Integer.parseInt(cursor.getString(2)));
                rank.setPant(Integer.parseInt(cursor.getString(3)));
                rank.setScore(Integer.parseInt(cursor.getString(4)));
                rank.setR(Integer.parseInt(cursor.getString(5)));
                // Adding contact to list
                rankList.add(rank);
            } while (cursor.moveToNext());
        }
        
        //PANTALLA 3
        selectQuery = "SELECT  * FROM " + TABLE_RANKING + " WHERE " + KEY_MUNDO + "= 3 AND " + KEY_PANTALLA + " = 3 ORDER BY "+ KEY_SCORE +" ASC";
        
        db = this.getWritableDatabase();
        cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Rank rank = new Rank();
                rank.setID(Integer.parseInt(cursor.getString(0)));
                rank.setUser(cursor.getString(1));
                rank.setMundo(Integer.parseInt(cursor.getString(2)));
                rank.setPant(Integer.parseInt(cursor.getString(3)));
                rank.setScore(Integer.parseInt(cursor.getString(4)));
                rank.setR(Integer.parseInt(cursor.getString(5)));
                // Adding contact to list
                rankList.add(rank);
            } while (cursor.moveToNext());
        }
 
        // return rank list
        return rankList;
    }
    
    // Getting All Contacts
    public List<Ant> getAllAnt() {
        List<Ant> antList = new ArrayList<Ant>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_ANT;
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Ant ant = new Ant();
                ant.setID(Integer.parseInt(cursor.getString(0)));
                ant.setUser(cursor.getString(1));
                ant.setCasco(Integer.parseInt(cursor.getString(2)));
                ant.setCuerpo(Integer.parseInt(cursor.getString(3)));
                ant.setColor(Integer.parseInt(cursor.getString(4)));
                ant.setColor(Integer.parseInt(cursor.getString(5)));
                // Adding contact to list
                antList.add(ant);
            } while (cursor.moveToNext());
        }
 
        // return contact list
        return antList;
    }
 
    
    // Updating single contact
    public int updateContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(KEY_USER, contact.getUser());
        values.put(KEY_PASS, contact.getPass());
        values.put(KEY_MUNDO, contact.getMundo());
        values.put(KEY_PANTALLA, contact.getPant());
        values.put(KEY_COLOR, contact.getIdcolor());
 
        // updating row
        return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
                new String[] { String.valueOf(contact.getID()) });
    }
 
    // Updating single file Ranking
    public int updateRank(Rank rank) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(KEY_USER, rank.getUser());
        values.put(KEY_MUNDO, rank.getMundo());
        values.put(KEY_PANTALLA, rank.getPant());
        values.put(KEY_SCORE, rank.getScore());
        values.put(KEY_R, rank.getR());
 
        // updating row
        return db.update(TABLE_RANKING, values, KEY_ID + " = ?",
                new String[] { String.valueOf(rank.getID()) });
    }
    
    
    // Updating single contact
    public int updateAnt(Ant ant) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(KEY_USER, ant.getUser());
        values.put(KEY_CASCO, ant.getCasco());
        values.put(KEY_CUERPO, ant.getCuerpo());
        values.put(KEY_COLOR, ant.getColor());
        values.put(KEY_BOTAS, ant.getBotas());
 
        // updating row
        return db.update(TABLE_ANT, values, KEY_ID + " = ?",
                new String[] { String.valueOf(ant.getID()) });
    }
    
    // Deleting single contact
    public void deleteContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
                new String[] { String.valueOf(contact.getID()) });
        db.close();
    }
    
    // Deleting single rank
    public void deleteRank(Rank rank) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_RANKING, KEY_ID + " = ?",
                new String[] { String.valueOf(rank.getID()) });
        db.close();
    }
    
    // Deleting single ant
    public void deleteAnt(Ant ant) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_ANT, KEY_ID + " = ?",
                new String[] { String.valueOf(ant.getID()) });
        db.close();
    }
 
    // Getting contacts Count
    public int getContactsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_CONTACTS;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
 
        // return count
        return cursor.getCount();
    }
    
    
    // Getting Ranking count
    public int getRankCount() {
        String countQuery = "SELECT  * FROM " + TABLE_RANKING;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        
 
        // return count
        return cursor.getCount();
    }
    
    // Getting ant Count
    public int getAntCount() {
        String countQuery = "SELECT  * FROM " + TABLE_ANT;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
 
        // return count
        return cursor.getCount();
    }

    // Adding new SCORE
    void IniRank() {
    	addScore(new Rank("Pepito", 1, 1, 1000, 0));
    	addScore(new Rank("Pepito", 1, 2, 100, 0));
    	addScore(new Rank("Juanito", 3, 1, 2000, 0));
    	addScore(new Rank("Pepito", 1, 3, 2000, 0));
    	addScore(new Rank("Pepito", 2, 1, 500, 0));
    	addScore(new Rank("Pepito", 1, 2, 2000, 0));
    }
    
 // Getting All Rankings Mundo 1
    public int BuscaAnt(int color, int cuerpo, int casco, int botas, String user) {
    	int id = -1;
        // Select All Query
        String selectQuery = "SELECT "+ KEY_ID + " FROM " + TABLE_ANT + " WHERE " + KEY_COLOR + "= " + color + " AND " + KEY_CUERPO + " = " +
        cuerpo + " AND " + KEY_CASCO + " = " + casco + " AND " + KEY_BOTAS + " = " + botas + " AND " + KEY_USER + " = '"+ user + "'";
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                id = Integer.parseInt(cursor.getString(0));
                // Adding contact to list
            } while (cursor.moveToNext());
        }
        // return rank list
        return id;
    }
    
 // Updating single contact
    public int updateContact(String user, int color) {
    	Contact contact = getContact(user);
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(KEY_USER, contact.getUser());
        values.put(KEY_PASS, contact.getPass());
        values.put(KEY_MUNDO, contact.getMundo());
        values.put(KEY_PANTALLA, contact.getPant());
        values.put(KEY_COLOR, color);
 
        // updating row
        return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
                new String[] { String.valueOf(contact.getID()) });
    }
    
 // Getting All Contacts
    public void UpdateAnt(String user) {
        List<Ant> antList = new ArrayList<Ant>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_ANT;
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
            	if (cursor.getString(1).equals(user)) return;
//                Ant ant = new Ant();
//                ant.setID(Integer.parseInt(cursor.getString(0)));
//                ant.setUser(cursor.getString(1));
//                ant.setCasco(Integer.parseInt(cursor.getString(2)));
//                ant.setCuerpo(Integer.parseInt(cursor.getString(3)));
//                ant.setColor(Integer.parseInt(cursor.getString(2)));
//                // Adding contact to list
//                antList.add(ant);
            	ContentValues values = new ContentValues();
                values.put(KEY_USER, user);
                values.put(KEY_CASCO, Integer.parseInt(cursor.getString(2)));
                values.put(KEY_CUERPO, Integer.parseInt(cursor.getString(3)));
                values.put(KEY_COLOR, Integer.parseInt(cursor.getString(4)));
                values.put(KEY_BOTAS, Integer.parseInt(cursor.getString(5)));
                db.update(TABLE_ANT, values, KEY_ID + " = ?",
                        new String[] { cursor.getString(0) });
            } while (cursor.moveToNext());
        }
    }
    
 // Adding new SCORE
    void IniAnt(String user) {
//    	int id = 0;
    	for(int i = 0; i < 4; ++i) {
    		for(int j = 0; j < 4; ++j) {
    			for(int k = 0; k < 4; ++k) {
    				for(int l = 0; l < 4; ++l) {
    					addAnt(new Ant(user, i, j, k, l));
//    					++id;
    				}
    			}
    		}
    	}
    }
    
    
}